using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;

namespace JNPF.Basics.Logic
{
    public class UserextendService: DataFactory, UserextendIService
    {
		/// <summary>
		/// 获取信息
		/// </summary>
		/// <param name="id">主键id</param>
		/// <returns></returns>
		public async Task<UserextendEntity> GetInfo(int id)
		{
			using (var db = this.Basic)
			{
				return await db.GetEntityAsync<UserextendEntity>(m => m.Id == id);
			}
		}

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns></returns>
        public async Task<UserextendEntity> GetInfoByUserId(string userId)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<UserextendEntity>(m => m.UserId == userId);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<UserextendEntity>> GetPageList(UserextendListQueryInput input)
		{
			using (var db = this.Basic)
			{
				var queryWhere = ExtLinq.True<UserextendEntity>();
				var queryParam = input.queryJson.ToObject();
				//查询用户主键
				if (!string.IsNullOrEmpty(input.userId))
				{
					queryWhere = queryWhere.And(m => m.UserId.Contains(input.userId));
				}
				//查询总获取积分
				if (input.totalPointsEarned.ToInt()!=0)
				{
					queryWhere = queryWhere.And(m => m.TotalPointsEarned == input.totalPointsEarned);
				}
				//查询已提现积分
				if (input.totalPointsSpent.ToInt()!=0)
				{
					queryWhere = queryWhere.And(m => m.TotalPointsSpent == input.totalPointsSpent);
				}
				//查询待提现积分
				if (input.pointsPendingWithdrawal.ToInt()!=0)
				{
					queryWhere = queryWhere.And(m => m.PointsPendingWithdrawal == input.pointsPendingWithdrawal);
				}


                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<UserextendEntity>().Where(queryWhere).GetPageResultAsync(input);
			}
		}

		/// <summary>
		/// 创建信息
		/// </summary>
		/// <param name="entity">实体对象</param>
		/// <returns></returns>
		public async Task<int> Create(UserextendEntity entity)
		{
			using (var db = this.Basic)
			{
				return await db.InsertAsync(entity);
			}
		}

		/// <summary>
		/// 更新信息
		/// </summary>
		/// <param name="entity">实体对象</param>
		/// <returns></returns>
		public async Task<int> Update(UserextendEntity entity)
		{
			using (var db = this.Basic)
			{
				return await db.UpdateAsync(entity);
			}
		}

		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="entity">实体对象</param>
		/// <returns></returns>
		public async Task<int> Delete(UserextendEntity entity)
		{
			using (var db = this.Basic)
			{
				return await db.DeleteAsync(entity);
			}
		}

		/// <summary>
		/// 列表
		/// </summary>
		/// <param name="input">请求参数</param>
		/// <returns></returns>
		public async Task<List<UserextendEntity>> GetList(UserextendListQueryInput input)
		{
			using (var db = this.Basic)
			{
				var queryWhere = ExtLinq.True<UserextendEntity>();
				//查询用户主键
				if (!string.IsNullOrEmpty(input.userId))
				{
					queryWhere = queryWhere.And(m => m.UserId.Contains(input.userId));
				}
				//查询总获取积分
				if (input.totalPointsEarned.ToInt()!=0)
				{
					queryWhere = queryWhere.And(m => m.TotalPointsEarned == input.totalPointsEarned);
				}
				//查询已提现积分
				if (input.totalPointsSpent.ToInt()!=0)
				{
					queryWhere = queryWhere.And(m => m.TotalPointsSpent == input.totalPointsSpent);
				}
				//查询待提现积分
				if (input.pointsPendingWithdrawal.ToInt()!=0)
				{
					queryWhere = queryWhere.And(m => m.PointsPendingWithdrawal == input.pointsPendingWithdrawal);
				}

                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<UserextendEntity>().Where(queryWhere).ToListAsync();
			}
		}


    }
}